package com.leyou.page.service.impl;

import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.leyou.page.constant.RedisConstant.*;

@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据spuId查询spu
     * @param spuId 商品id
     * @return 商品信息
     */
    @Override
    public String querySpuById(Long spuId) {
        //查询数据
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        if (spuDTO == null || spuDTO.getId() == null) {
            throw new LyException(404, "商品不存在！");
        }
        //转为vo

        SpuVO spuVO = spuDTO.toEntity(SpuVO.class);

        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        //v转json
        String json = JsonUtils.toJson(spuVO);
        //将数据写入redis
        stringRedisTemplate.opsForValue().set(SPU_KEY_PREFIX+spuId,json);
        //返回
        return json;
    }

    /**
     * 查询商品sku数据
     * @param spuId 商品id
     * @return sku数据
     */
    @Override
    public String loadSkuListData(Long spuId) {
        //查询数据
        List<SkuDTO> skuDTOList = itemClient.querySkuDTOListById(spuId);
        if (skuDTOList == null || skuDTOList.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
            //转为json
            String json = JsonUtils.toJson(skuDTOList);
        //存入redis
        stringRedisTemplate.opsForValue().set(SKU_KEY_PREFIX+spuId,json);

        return json;
    }

    /**
     * 查询商品spuDetail数据
     * @param spuId 商品id
     * @return spu数据
     */
    @Override
    public String loadSpuDetailData(Long spuId) {
        //查询
        SpuDetailDTO spuDetailDTO = itemClient.querySpuDetailById(spuId);
        if (spuDetailDTO == null) {
            throw new LyException(404, "商品不存在！");
        }
        //转json
        String json = JsonUtils.toJson(spuDetailDTO);
        //存入redis
        stringRedisTemplate.opsForValue().set(DETAIL_KEY_PREFIX+spuId,json);

        return json;
    }
    /**
     * 查询商品分类数据
     * @param ids 商品分类id
     * @return 分类数据
     */
    @Override
    public String loadCategoriesData(List<Long> ids) {
        //查询
        List<CategoryDTO> dtoList = itemClient.queryCategoryByIds(ids);
        if (dtoList == null || dtoList.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        //转vo
        List<CategoryVO> collect = dtoList.stream()
                .map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class))
                .collect(Collectors.toList());

        //转为json
        String json = JsonUtils.toJson(collect);
        //写入redis
        stringRedisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX+ids.get(2),json);

        //返回
        return json;
    }

    /**
     * 查询品牌数据
     * @param id 品牌id
     * @return spu数据
     */
    @Override
    public String loadBrandData(Long id) {
        //查询
        BrandDTO brandDTO = itemClient.queryBrandById(id);
        if (brandDTO == null) {
            throw new LyException(404, "商品不存在！");
        }
        // 转换vo
        BrandVO brandVO = brandDTO.toEntity(BrandVO.class);
        //转换为字符串
        String json = JsonUtils.toJson(brandVO);

        //存入redis
        stringRedisTemplate.opsForValue().set(BRAND_KEY_PREFIX+id,json);

        return json;
    }
    /**
     * 查询规格数据
     * @param categoryId 分类id
     * @return 规格参数
     */
    @Override
    public String loadSpecData(Long categoryId) {
        //查询
        List<SpecGroupDTO> specGroupDTOList = itemClient.queryGroupAndParamByCategoryId(categoryId);
        if (specGroupDTOList == null || specGroupDTOList.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        //转vo
        List<SpecGroupVO> groupVOList = new ArrayList<>(specGroupDTOList.size());
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            //创建vo
            SpecGroupVO specGroupVO = new SpecGroupVO();
            //填写属性
            specGroupVO.setName(specGroupDTO.getName());
            List<SpecParamVO> params = specGroupDTO.getParams().stream()
                    .map(specParamDTO -> specParamDTO.toEntity((SpecParamVO.class)))
                    .collect(Collectors.toList());
            specGroupVO.setParams(params);
            //放入集合
            groupVOList.add(specGroupVO);
        }
        //序列化为json
        String json = JsonUtils.toJson(groupVOList);
        //写入redis
        stringRedisTemplate.opsForValue().set(SPEC_KEY_PREFIX+categoryId,json);
        //返回
        return json;
    }

    @Override
    public Boolean deleteSku(Long spuId){
        return stringRedisTemplate.delete(SKU_KEY_PREFIX + spuId);
    }

    @Override
    public Boolean deleteBrand(Long brandId) {
        return stringRedisTemplate.delete(BRAND_KEY_PREFIX+brandId);
    }


}
